package com.cqkjxy.controller;

import com.cqkjxy.commons.constants.CommonConstants;
import com.cqkjxy.entity.StaffEntity;
import com.cqkjxy.service.StaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class StaffController {

    @Autowired
    private StaffService staffService;

    @RequestMapping("/staff_manage")
    public String staffManage(Model model) {
        return "pages/staff/staff_content";
    }

    @RequestMapping("/staff_list")
    public String staffList(Model model, @RequestParam(defaultValue = "1") int page) {
        findAllStaff(model, page - 1);
        return "pages/staff/staff_manage_list";
    }

    private void findAllStaff(Model model, int i) {
        Page<StaffEntity> list = staffService.findList(i, CommonConstants.PAGE_SIZE);
        model.addAttribute("staffPage", list);
    }

    @RequestMapping("/staff_add")
    public String staffAdd(Model model, @RequestParam(required = false, defaultValue = "-1") int pkId, String name, String dep) {
        StaffEntity staffEntity = new StaffEntity();
        if (pkId != -1) {
            staffEntity.setPkId(pkId);
        }
        staffEntity.setStaffName(name);
        staffEntity.setStaffDep(dep);
        int save = staffService.save(staffEntity);
        findAllStaff(model, 0);
        return "pages/staff/staff_manage_list";
    }

    @RequestMapping("/staff_delete")
    public String staffDel(Model model, int pkId) {
        staffService.deleteRelation(pkId);
        findAllStaff(model, 0);
        return "pages/staff/staff_manage_list";
    }

}
